package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.FoodInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;
import com.xgkj.diyiketang.http.engine.TaskResultCode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/11.
 */
public class FoodListTask extends CouponHttpBaseTask {
    private int mSize;
    private int mPageNo;
    private String mArea;
    private static final String FOODLIST = "foodList";
    public FoodListTask(int size, int pageNo,String area){
        super(TaskType.FOODLIST_TASK);
        mSize = size;
        mPageNo = pageNo;
        if(area==null){
            area="北京市";
        }
        mArea = area;

    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject respJson = response.getBody();
        int pageCount = 0;
        int currentPage = 0;
        int nextPage = 0;
        List<FoodInfo> list = new ArrayList<FoodInfo>();
        JSONObject jsonString = null;
        if (respJson != null) {
           jsonString = respJson.getJSONObject(FOODLIST);
//            currentPage = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
//            pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
//            nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
//            list = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), FoodInfo.class);
        }

        notifyTaskCompleted(TaskResultCode.SUCCESS, jsonString);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT) {
            CouponHttpRequest request = new CouponHttpRequest(RequestType.FOODLIST_REQUEST);
            JSONObject json = new JSONObject();
            json.put("size", mSize);
            json.put("pageNo", mPageNo);
            json.put("areaName", mArea);
            request.setData(json.toString());
            request.setApiNo(ApiNo.FOODLIST);
            addRequest(request);
        }
    }
}
